﻿/// <reference path="SimpleControl.js" />
/// <reference path="../ListControl/BaseListControl.js" />

var ____a;

var $SelectControl = TA.Turbo.Controls.SelectControl = function SelectControl(clientId)
{
	this.SimpleControl(clientId);
	this.BaseListControl(clientId);
}
$SelectControl.Inherit($SimpleControl);
$SelectControl.Inherit($BaseListControl);

if (____a)
{
	$SelectControl.prototype = new $BaseListControl();
	$SelectControl.prototype.SimpleControl = new $SimpleControl();
	$SelectControl.prototype.BaseListControl = new $BaseListControl();
}

$SelectControl.prototype.get_Value = function get_Value()
{
	var sb = new StringBuilder(), first = true;
	var o = this.get_Control().options, ol = o.length;
	for (var iOption = 0; iOption < ol; ++iOption)
	{
		var option = o[iOption];

		if (option.selected)
		{
			if (!first)
				sb.Append(",");
			first = false;

			sb.Append(option.value);
		}
	}
	return sb.toString();
}
$SelectControl.prototype.set_Value = function set_Value(value)
{
	var mcl = new RegExp("[^,]+").Matches(value);
	var o = this.get_Control().options, ol = o.length;
	for (var iOption = 0; iOption < ol; ++iOption)
	{
		var option = o[iOption];

		option.selected = (mcl.IndexWhere(function(m) { return m[0] == option.value; }) != -1);
	}
}


$SelectControl.prototype.Init = function Init()
{
	this.SimpleControl.Init.call(this);

	this.get_Control().AddEvent("change", this._OnValueChanged, this);
}



$SelectControl.prototype._ItemsGetInitValue_Fill = function _ItemsGetInitValue_Fill(collection, container)
{
	var cn = container.childNodes, cnl = cn.length;
	for (var i = 0; i < cnl; ++i)
	{
		var child = cn[i];

		if (child.tagName != "OPTION" && child.tagName != "OPTGROUP")
			continue;
		var item = new $ListItem();
		item.ReadAttributes(child);

		if (child.tagName == "OPTGROUP")
		{
			item.Text = child.getAttribute("label");

			this._ItemsGetInitValue_Fill(item.Items, child);
		}
		else
		{
			item.Text = child.innerHTML;
			item.Value = child.value;
		}

		collection.push(item);
	}
}
$SelectControl.prototype._CreateGroupElements = function _CreateGroupElements(item)
{
	if (_____a)
		item = new $ListItem();

	var group = document.createElement("OPTGROUP");
	group.setAttribute("label", item.Text);


	return [group];
}

$SelectControl.prototype._CreateOptionElements = function _CreateOptionElements(item)
{
	if (_____a)
		item = new $ListItem();

	var option = document.createElement("OPTION");
	option.value = item.Value;
	item.AddAttributes(option);
	return [option];
}
